/**
 *  *2010-10-28 下午12:31:49  add 
 */
package com.base.myproject.client.init;


import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.BaseTreeModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.TreeModel;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * @author Administrator
 *2010-10-28
 * javazgw@gmail.com
 */
public class Test3  extends Window {

	public Test3() {
		setSize(640, 480);
		setLayout(new FitLayout());
	}

	@Override
	protected void onRender(Element parent, int pos) {
		FormPanel formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);

		ListStore<ModelData> store = new ListStore<ModelData>();
		ModelData md = new BaseModelData();
		md.set("id", "");
		md.set("name", "");
		store.add(md);

		ComboBox<ModelData> cmbxParent = new ComboBox<ModelData>();
		cmbxParent.setFieldLabel("parent");
		cmbxParent.setStore(store);
		cmbxParent.setDisplayField("name");
		cmbxParent.setTriggerAction(TriggerAction.ALL);
		cmbxParent.setTemplate(getTemplate());
		formPanel.add(cmbxParent, new FormData("100%"));
		add(formPanel);

		final ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		panel.setLayout(new FitLayout());
		panel.setBodyBorder(false);
		panel.setHeight(300);

		final TreePanel<TreeModel> tree = new TreePanel<TreeModel>(
				getTreeStore());
		tree.setDisplayProperty("name");
		panel.add(tree);

		cmbxParent.addListener(Events.BeforeQuery, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				// // com.google.gwt.user.client.Window.alert("事件");
				
				RootPanel.get("tree1").add(panel);
			}
		});
		
//		lbp 
//		hp1000
		super.onRender(parent, pos);
	}

	private native String getTemplate() /*-{
		return [
		'<tpl for=".">',
		'<div id="tree1" style=" height:150px;"></div>',
		'</tpl>' 
		].join("");
	}-*/;

	private TreeStore<TreeModel> getTreeStore() {
		TreeStore<TreeModel> store = new TreeStore<TreeModel>();

		TreeModel root = new BaseTreeModel();
		root.set("name", "根");

		TreeModel t = new BaseTreeModel();
		t.set("name", "第一");
		root.add(t);
		t.setParent(root);

		t = new BaseTreeModel();
		t.set("name", "第二");
		root.add(t);
		t.setParent(root);

		store.add(root, true);
		return store;
	}
}
